#include "pch.h"

using namespace Windows::UI::Core;
using namespace Windows::UI::Input;
using namespace Windows::UI;
using namespace Windows::Foundation;
using namespace Microsoft::WRL;
using namespace Windows::Devices::Input;
using namespace Windows::System;

StateBuilding::StateBuilding()
{

}

StateBuilding::~StateBuilding()
{

}

// enter state
void StateBuilding::OnEnter()
{
	Box2DManager::Create();
	RobotManager::Create();
	ActionManager::Create();
	BuildingScene::Create();

	BuildingScene::Instance()->Initialize();

	Box2DManager::Instance()->SetContactListener(BuildingScene::Instance()->GetContactListener());
}

// leave state
void StateBuilding::OnLeave()
{
	BuildingScene::Destory();
	ActionManager::Destory();
	RobotManager::Destory();
	Box2DManager::Destory();
}

// update
void StateBuilding::OnUpdate(float frameTime)
{
	RobotManager::Instance()->OnUpdate(frameTime);
	ActionManager::Instance()->OnUpdate(frameTime);
	Box2DManager::Instance()->OnUpdate(frameTime);
	BuildingScene::Instance()->OnUpdate(frameTime);

	switch (InputManager::Instance()->GetPointPressStatus())
	{
	case PointStatus_Two:
		{
			PointInfo& point_infoA = InputManager::Instance()->GetPressedPointAt(0);
			PointInfo& point_infoB = InputManager::Instance()->GetPressedPointAt(1);
			PointerPoint^ pointA = PointerPoint::GetCurrentPoint(point_infoA.point_id);
			PointerPoint^ pointB = PointerPoint::GetCurrentPoint(point_infoB.point_id);

			if(pointA && pointB)
			{
				float2 pA= float2(pointA->Position.X, pointA->Position.Y);
				float2 pB= float2(pointB->Position.X, pointB->Position.Y);

				Camera2D^ current_camera = DirectX2DRender::Instance()->GetCurrentCamera();

				float lengthdelta = length(pA - pB) - length(point_infoA.last_position - point_infoB.last_position);


				current_camera->ZoomIn(lengthdelta * camera_z_step * 0.1f);

				point_infoA.last_position = pA;
				point_infoB.last_position = pB;
			}
		}
		break;
	case PointStatus_One:
		{
			PointInfo& point_infoA = InputManager::Instance()->GetPressedPointAt(0);
			PointerPoint^ pointA = PointerPoint::GetCurrentPoint(point_infoA.point_id);

			float2 pA= float2(pointA->Position.X, pointA->Position.Y);

			float2 delta = pA - point_infoA.last_position;

			delta.y = - delta.y;

			Camera2D^ current_camera = DirectX2DRender::Instance()->GetCurrentCamera();

			current_camera->Move(delta * camera_move_speed * 0.1f);

			point_infoA.last_position = pA;

		}
		break;
	default:
		break;
	}
}

// render
void StateBuilding::OnPreRender()
{
	RobotManager::Instance()->OnPreRender();
	BuildingScene::Instance()->OnPreRender();
}

bool StateBuilding::OnKeyDown(const Windows::System::VirtualKey& Key)
{
	if(Key == VirtualKey::Number1)
	{
		
	}

	return false;
}

bool StateBuilding::OnKeyUp(const Windows::System::VirtualKey& Key)
{
	return false;
}

bool StateBuilding::OnPointerMoved(Windows::UI::Core::PointerEventArgs^ args)
{
	Collections::IVector<PointerPoint^>^ history = args->GetIntermediatePoints();
	PointerPoint^ current = args->CurrentPoint;
	if(history->Size > 0)
	{
		
	}
	return true;
}

bool StateBuilding::OnPointerPressed(Windows::UI::Core::PointerEventArgs^ args)
{
	Collections::IVector<PointerPoint^>^ history = args->GetIntermediatePoints();
	PointerPoint^ current = args->CurrentPoint;
	if(history->Size > 0)
	{
		int a = history->Size ;
		int b = 0;
	}

	return true;
}

bool StateBuilding::OnPointerReleased(Windows::UI::Core::PointerEventArgs^ args)
{
	return false;
}